package com.shangyitong.yygh_hosp.service.impl.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangyitong.ResultCodeEnum;
import com.shangyitong.exception.HospitalException;
import com.shangyitong.helper.HttpRequestHelper;
import com.shangyitong.util.MapToJavaObjectWithClass;
import com.shangyitong.yygh.model.hosp.Hospital;
import com.shangyitong.yygh.model.hosp.HospitalSet;
import com.shangyitong.yygh.model.hosp.Schedule;
import com.shangyitong.yygh.vo.hosp.HospitalQueryVo;
import com.shangyitong.yygh_hosp.mapper.HospitalSetMapper;
import com.shangyitong.yygh_hosp.repository.HospitalRepository;
import com.shangyitong.yygh_hosp.service.HospitalRearService;
import com.shangyitong.yygh_hosp.service.api.HospitalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Autowired
    private HospitalRearService hospitalRearService;

    @Override
    public void save(Map<String, String[]> parameterMap) {
        Hospital hospital = JSONObject.parseObject(MapToJavaObjectWithClass.mapToJavaObject(parameterMap), Hospital.class);
        String hoscode = hospital.getHoscode();
        //检测hoscode是否为空
        if (StringUtils.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.DATA_ISNULL);
        }
        //检测后台是否有该医院
        HospitalSet hospitalSet = hospitalSetMapper.getByHoscode(hoscode);
        if (hospitalSet == null) {
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        } else {
            //校对签名
            if (!HttpRequestHelper.isSignEquals(MapToJavaObjectWithClass.switchObjectMap(parameterMap),
                    hospitalSet.getSignKey())) {
                throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        //查看医院中是否有该医院
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalExist != null) {
            //若存在该医院，则修改
            hospital.setStatus(hospitalExist.getStatus());
            hospitalExist.setUpdateTime(new Date());
            hospitalRepository.save(hospitalExist);
        } else {
            //若不存在该医院，则添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(Map<String, String[]> parameterMap) {
        Hospital hospital = JSONObject.parseObject(MapToJavaObjectWithClass.mapToJavaObject(parameterMap), Hospital.class);
        String hoscode = hospital.getHoscode();
        if (StringUtils.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        HospitalSet hospitalSet = hospitalSetMapper.getByHoscode(hoscode);
        if (hospitalSet == null) {
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        if (!HttpRequestHelper.isSignEquals(MapToJavaObjectWithClass.switchObjectMap(parameterMap),
                hospitalSet.getSignKey())) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> getByHosname(String hosname) {
        List<Hospital> hospitals = hospitalRepository.getHospitalByhosnameLike(hosname);
        if (hospitals != null) {
            return hospitals;
        }
        return null;
    }

    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        Hospital hospitalSetting = hospitalRearService.extractedHospital(hospital);
        Map<String,Object> result = new HashMap<>();
        result.put("hospital",hospitalSetting);
        result.put("bookingRule",hospitalSetting.getBookingRule());
        return result;
    }


}
